package com.itheima.array;

import java.util.Random;

/**
 * @author jiangxun
 * @date 11-14-2024
 */
public class LandlordGame {
    public static void main(String[] args) {
        // 定义一个数组存储54张牌
        String[] cards=new String[54];
        // 初始化牌
        initCards(cards);
        // 打印初始牌组
        System.out.println("初始牌组：");
        printCards(cards);
        //洗牌
        shuffleCards(cards);
        // 打印洗牌后的牌组
        System.out.println("洗牌后：");
        printCards(cards);

    }

    private static void initCards(String[] cards) {
        String[] suits={"♠","♥","♣","♦"};
        String[] ranks={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        int index=0;
        for(String suit:suits){
            for(String rank:ranks){
                cards[index]=suit+rank;
                index++;
            }
        }
        cards[index++]="👶";
        cards[index]="👴";
    }

    private static void shuffleCards(String[] cards) {
        Random random = new Random();
        // 生成一个随机整数 j，其值在 0 到 i 之间（包含 0 和 i）。
        // 这个 j 将用于确定与当前元素 i 进行交换的位置。
        for(int i=cards.length-1;i>0;i--){
            int j=random.nextInt(i+1);
            String temp=cards[i];
            cards[i]=cards[j];
            cards[j]=temp;
        }
    }

    private static void printCards(String[] cards) {
        for(int i=0;i<cards.length;i++){
            System.out.print(cards[i]+" ");
            if((i+1)%13==0){
                System.out.println();
            }
        }
        System.out.println();
    }
}
